package com.github.beansoft.jadx.gui.idea.ui;

import com.intellij.ide.ui.UISettings;
import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl;
import com.intellij.ui.JBColor;
import com.intellij.ui.paint.LinePainter2D;
import com.intellij.ui.scale.JBUIScale;
import com.intellij.util.ui.JBDimension;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * Refs com.intellij.openapi.actionSystem.impl.ActionToolbarImpl$MySeparator
 */
public class FlatToolBarSeparator extends JComponent {
    private final String myText;
    private int myOrientation = SwingConstants.HORIZONTAL;

    public FlatToolBarSeparator() {
       this(null);
    }

    public FlatToolBarSeparator(String text) {
        myText = text;
        setFont(JBUI.Fonts.toolbarSmallComboBoxFont());
    }

    protected @NotNull
    Color getSeparatorColor() {
        return JBUI.CurrentTheme.CustomFrameDecorations.separatorForeground();
    }

    protected int getSeparatorHeight() {
        return JBUIScale.scale(24);
    }

    public Dimension getSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getPreferredSize() {
        int gap = JBUIScale.scale(2);
        int center = JBUIScale.scale(3);
        int width = gap * 2 + center;
        int height = getSeparatorHeight();

        if (myOrientation == SwingConstants.HORIZONTAL) {
            if (myText != null) {
                FontMetrics fontMetrics = getFontMetrics(getFont());

                int textWidth = getTextWidth(fontMetrics, myText, getGraphics());
                return new JBDimension(width + gap * 2 + textWidth,
                        Math.max(fontMetrics.getHeight(), height), true);
            } else {
                return new JBDimension(width, height, true);
            }
        } else {
            //noinspection SuspiciousNameCombination
            return new JBDimension(height, width, true);
        }
    }

    @Override
    protected void paintComponent(final Graphics g) {
        if (getParent() == null) return;

        int gap = JBUIScale.scale(2);
        int center = JBUIScale.scale(3);
        int offset = 0;
//      if (myOrientation == SwingConstants.HORIZONTAL) {
//        offset = ActionToolbarImpl.this.getHeight() - getMaxButtonHeight() - 1;
//      }
//      else {
//        offset = ActionToolbarImpl.this.getWidth() - getMaxButtonWidth() - 1;
//      }

        g.setColor(getSeparatorColor());
        if (myOrientation == SwingConstants.HORIZONTAL) {
        int y2 = getHeight() - gap * 2 - offset;
        LinePainter2D.paint((Graphics2D)g, center, gap, center, y2);

//        if (myText != null) {
//          FontMetrics fontMetrics = getFontMetrics(getFont());
//          int top = (getHeight() - fontMetrics.getHeight()) / 2;
//          UISettings.setupAntialiasing(g);
//          g.setColor(JBColor.foreground());
//          g.drawString(myText, gap * 2 + center + gap, top + fontMetrics.getAscent());
//        }
        } else {
            LinePainter2D.paint((Graphics2D) g, gap, center, getWidth() - gap * 2 - offset, center);
        }
    }

    private int getTextWidth(@NotNull FontMetrics fontMetrics, @NotNull String text, @Nullable Graphics graphics) {
        if (graphics == null) {
            return fontMetrics.stringWidth(text);
        } else {
            Graphics g = graphics.create();
            try {
                UISettings.setupAntialiasing(g);
                return fontMetrics.getStringBounds(text, g).getBounds().width;
            } finally {
                g.dispose();
            }
        }
    }
}